/*
*********************************************************************************************************
*                                              EXAMPLE CODE
*
*                          (c) Copyright 2003-2017 Micrium, Inc.; Weston, FL
*
*               All rights reserved.  Protected by international copyright laws.
*               Knowledge of the source code may NOT be used to develop a similar product.
*               Please help us continue to provide the Embedded community with the finest
*               software available.  Your honesty is greatly appreciated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                            EXAMPLE CODE
*
*                                             TI MSP432 
*                                              on the 
*                                         TI MSP-EXP432P401R
*                                      LaunchPad Development Kit
*
* Filename      : app.c
* Version       : V1.00
* Programmer(s) : YS
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/

#include  <cpu_core.h>
#include  <os.h>
#include  <os_cfg.h>
#include  <bsp.h>
#include  <bsp_int.h>
#include  <bsp_sys.h>
#include  <app_cfg.h>
#include  <cpu_cfg.h>
#include  <lib_mem.h>
#include  <lib_math.h>
#include  <stdio.h>

#include  <system_gd32f4xx.h>
#include  <gd32f4xx.h>
#include "osObjects.h" 

/*
*********************************************************************************************************
*                                            LOCAL DEFINES
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                          GLOBAL VARIABLES
*********************************************************************************************************
*/

CPU_INT16U    App_LED1_Dly_ms;                                  /* Startup Task Delay. Global variable.                 */


/*
*********************************************************************************************************
*                                       LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/

static  OS_TCB        App_TaskStartTCB;
static  CPU_STK_SIZE  App_TaskStartStk[APP_CFG_TASK_START_STK_SIZE];


/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void  App_TaskStart          (void  *p_arg);

void PortInit(void);

/*
*********************************************************************************************************
*                                                main()
*
* Description : This is the standard entry point for C code.  It is assumed that your code will call
*               main() once you have performed all necessary initialization.
*
* Argument(s) : none.
*
* Return(s)   : none.
*
* Caller(s)   : This the main standard entry point.
*
* Note(s)     : none.
*********************************************************************************************************
*/

int  main (void)
{
    OS_ERR  err;

    PortInit();
    
    SystemCoreClockUpdate();
    
    Mem_Init();                                                 /* Initialize Memory Managment Module                   */
    
    CPU_IntDis();                                               /* Disable all Interrupts.                              */

    OSInit(&err);                                               /* Init uC/OS-III.                                      */


    OSTaskCreate((OS_TCB     *)&App_TaskStartTCB,               /* Create the start task                                */
                 (CPU_CHAR   *)"App Task Start",
                 (OS_TASK_PTR ) App_TaskStart,
                 (void       *) 0,
                 (OS_PRIO     ) APP_CFG_TASK_START_PRIO,
                 (CPU_STK    *)&App_TaskStartStk[0],
                 (CPU_STK     )(APP_CFG_TASK_START_STK_SIZE / 10u),
                 (CPU_STK_SIZE) APP_CFG_TASK_START_STK_SIZE,
                 (OS_MSG_QTY  ) 0,
                 (OS_TICK     ) 0,
                 (void       *) 0,
                 (OS_OPT      )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
                 (OS_ERR     *)&err);

    OSStart(&err);                                              /* Start multitasking (i.e. give control to uC/OS-III). */
}


/*
*********************************************************************************************************
*                                          App_TaskStart()
*
* Description : This is an example of a startup task.  As mentioned in the book's text, you MUST
*               initialize the ticker only once multitasking has started.
*
* Argument(s) : p_arg   is the argument passed to 'App_TaskStart()' by 'OSTaskCreate()'.
*
* Return(s)   : none.
*
* Caller(s)   : This is a task.
*
* Notes       : (1) The first line of code is used to prevent a compiler warning because 'p_arg' is not
*                   used.  The compiler should not generate any code for this statement.
*********************************************************************************************************
*/

static  void  App_TaskStart (void *p_arg)
{
    OS_ERR       os_err;


   (void)p_arg;                                                 /* See Note #1                                          */

    BSP_Init();                                                 /* Start BSP and tick initialization                    */
    BSP_Tick_Init();


    Math_Init();

#ifdef CPU_CFG_INT_DIS_MEAS_EN
    CPU_IntDisMeasMaxCurReset();
#endif

    APP_TRACE_INFO(("Creating Application Events...\n\r"));
    AppObjCreate();                                            /* Create Applicaiton kernel objects                    */

    APP_TRACE_INFO(("Creating Application Tasks...\n\r"));
    AppTaskCreate();                                           /* Create Application tasks                             */

    App_LED1_Dly_ms = 100u;

    while (DEF_TRUE) {                                          /* Task body, always written as an infinite loop.       */
        OSTimeDly (10000, OS_OPT_TIME_DLY, &os_err);
    }
}

/*****************************************************************************//*!
* @brief   	port init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void PortInit()
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_reset_enable(RCU_GPIOARST);
    rcu_periph_reset_disable(RCU_GPIOARST);
    
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_reset_enable(RCU_GPIOBRST);
    rcu_periph_reset_disable(RCU_GPIOBRST);
    
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_reset_enable(RCU_GPIOCRST);
    rcu_periph_reset_disable(RCU_GPIOCRST);
    
    rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_reset_enable(RCU_GPIODRST);
    rcu_periph_reset_disable(RCU_GPIODRST);
    
    rcu_periph_clock_enable(RCU_GPIOE);
    rcu_periph_reset_enable(RCU_GPIOERST);
    rcu_periph_reset_disable(RCU_GPIOERST);
    
    rcu_periph_clock_enable(RCU_GPIOF);
    rcu_periph_reset_enable(RCU_GPIOFRST);
    rcu_periph_reset_disable(RCU_GPIOFRST);
    
    rcu_periph_clock_enable(RCU_GPIOG);
    rcu_periph_reset_enable(RCU_GPIOGRST);
    rcu_periph_reset_disable(RCU_GPIOGRST);
    
    rcu_periph_clock_enable(RCU_GPIOH);
    rcu_periph_reset_enable(RCU_GPIOHRST);
    rcu_periph_reset_disable(RCU_GPIOHRST);
    
    rcu_periph_clock_enable(RCU_GPIOI);
    rcu_periph_reset_enable(RCU_GPIOIRST);
    rcu_periph_reset_disable(RCU_GPIOIRST);
    
    //LED
    gpio_mode_set(GPIOB,GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_3 | GPIO_PIN_4);
    gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_3 | GPIO_PIN_4);
    //LED
    gpio_mode_set(GPIOD,GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_7);
    gpio_output_options_set(GPIOD,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_7);
    //KEY
    gpio_mode_set(GPIOA,GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
    gpio_output_options_set(GPIOA,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_0);
    gpio_mode_set(GPIOE,GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0 | GPIO_PIN_1);
    gpio_output_options_set(GPIOE,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_0 | GPIO_PIN_1);

}

/*****************************************************************************//*!
* @brief   	Cpu init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void CpuInit(void){
  
}

/*****************************************************************************//*!
* @brief   	data init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void DataInit(void){
  
}


/*****************************************************************************//*!
* @brief   	system tick hook.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SysTickHook()
{
    
}